<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DataController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\DatabaseController;
use App\Http\Controllers\TableController;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::group(['middleware' => ['api.admin']],function() {
    Route::post('user/login',           [UserController::class, 'login']);
    Route::post('user/info',            [UserController::class, 'info']);
    Route::post('user/logout',          [UserController::class, 'logout']);

    /*数据库管理*/
    Route::post('database',             [DatabaseController::class, 'index']);
    Route::post('database/detail',      [DatabaseController::class, 'detail']);
    Route::post('database/update',      [DatabaseController::class, 'update']);
    Route::post('database/create',      [DatabaseController::class, 'create']);
    Route::post('database/delete',      [DatabaseController::class, 'delete']);
    Route::post('database/setDefault',  [DatabaseController::class, 'setDefault']);

    /*表格管理*/
    Route::post('table',                [TableController::class, 'index']);
    Route::post('table/detail',         [TableController::class, 'detail']);
    Route::post('table/create',         [TableController::class, 'create']);
    Route::post('table/update',         [TableController::class, 'update']);
    Route::post('table/delete',         [TableController::class, 'delete']);
    Route::post('table/del-column',     [TableController::class, 'deleteColumn']);
});



/*==自动API==*/
Route::group(['middleware' => ['api.database']], function() {
    Route::get('auto/{database}/{table}',          [DataController::class, 'index']);  // 查 多
    Route::get('auto/{database}/{table}/{id}',     [DataController::class, 'detail']); // 查 单
    Route::put('auto/{database}/{table}',          [DataController::class, 'create']); // 增
    Route::post('auto/{database}/{table}/{id}',    [DataController::class, 'update']); // 改
    Route::delete('auto/{database}/{table}/{id}',  [DataController::class, 'delete']); // 删
});


Route::post('upload', [\App\Http\Controllers\UploadController::class, 'save']);
